<?php

class home
{
    public function __construct()
    {
        //微信与业务服务器验证
        Wechat::valid();
    }

    /*微信服务器接口地址*/
    public function index()
    {
        //如果用户是订阅
        if (Wechat::isSubscribe()) {
            //给用户回复订阅之后的信息
            Wechat::replyMsg('欢迎你订阅我！更多精彩等你发现哦');

        }

        //点击菜单
        if (Wechat::isEventKey()) {
            $keyValue = Wechat::getClickKey();

            //手机归属地查询
            if (preg_match("/手机归属地查询/", $keyValue)) {
                $res = '您可以直接输入电话号码查询归属地等信息哦^_^';
            }

            //笑话
            if (preg_match("/笑话/", $keyValue)) {
                $res = Joke::getJoke();
            }

            //周公解梦
            if (preg_match("/周公解梦/", $keyValue)) {
                $res = '周公解梦使用格式示例：
解梦黄金';
            }

            //抽签
            if (in_array($keyValue, ['观音灵签', '月老灵签', '财神爷灵签'])) {
                $res = Talk::chat(trim($keyValue));
            }
        }

        //根据发送内容回复
        //查询手机归属地
        if (preg_match("/1[34578]\d{9}/", Wechat::getContent(), $mobiles)) {
            $res = Mobile::getMobileAddr($mobiles[0]);
        }

        //周公解梦
        if (preg_match("/^解梦(.+)/", Wechat::getContent(), $dreams)) {
            $res = Dream::getDream(trim($dreams[1]));
        }


        //为空调用聊天机器人
        if (empty($res)) {
            $res = Talk::chat(trim(Wechat::getContent()));
        }
        $res = $res ?: '我们收到了您的信息';
        Wechat::replyMsg($res);
    }

    //获取微信服务器ip
    public function getip()
    {
        $data = Wechat::getIps();
        $data = json_decode($data);
        //输出ip地址，微信服务器很多ip地址
        foreach ($data->ip_list as $ip) {
            echo $ip . '<br/>';
        }
    }

    //创建菜单
    public function createMenu()
    {
        $data = <<<PHP
{
     "button":[
                {    
                  "type":"click",
                  "name":"每日一笑",
                  "key":"笑话"
                },
                {
                   "name":"生活小工具",
                   "sub_button":[        
                        {
                           "type":"click",
                           "name":"手机归属地查询",
                           "key":"手机归属地查询"
                        },
                        {
                           "type":"click",
                           "name":"周公解梦",
                           "key":"周公解梦"
                        },
                        {
                           "type":"click",
                           "name":"观音灵签",
                           "key":"观音灵签"
                        },
                        {
                           "type":"click",
                           "name":"月老灵签",
                           "key":"月老灵签"
                        },
                        {
                           "type":"click",
                           "name":"财神爷灵签",
                           "key":"财神爷灵签"
                        }
                    ]
                },
                {    
                  "type":"view",
                  "name":"SDK接口入口",
                  "url":"http://zhuoxin.mazhenyu.com/index.php?c=jssdk"
                }
              ]
 }
PHP;
        $res = Wechat::createMenu($data);
        p($res->response);
    }

}
























